﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
using System.Runtime.CompilerServices;


public partial class ExpensesServerSideBind : System.Web.UI.Page
{


    MySqlConnection conn = new MySqlConnection("Server=184.106.222.177;Database=exp;Uid=exp;Pwd=exp123;");
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void LoadData()
    {
        MySqlDataAdapter da = new MySqlDataAdapter();
        da.SelectCommand = new MySqlCommand("SELECT * FROM expenses", conn);
        DataTable dt = new DataTable();
        conn.Open();
        try
        {
            da.Fill(dt);
        }
        finally
        {
            conn.Close();
        }
        this.grdExpenses.DataSource = dt;
    }
    protected void grdExpenses_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        LoadData();
    }
    protected void grdExpenses_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        TextBox txtExpID = (TextBox)e.Item.FindControl("txtExpID");
        TextBox txtCategory = (TextBox)e.Item.FindControl("txtCategory");
        TextBox txtDescription = (TextBox)e.Item.FindControl("txtDescription");
        TextBox txtAmount = (TextBox)e.Item.FindControl("txtAmount");

        string strInsert = "INSERT INTO expenses(ExpID,Category,Description,Amount)VALUES('" + txtExpID.Text + "','" + txtCategory.Text + "','" + txtDescription.Text + "','" + txtAmount.Text + "')";
        MySqlCommand cmd = new MySqlCommand(strInsert, conn);

        conn.Open();
        try
        {
            cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
    }

    protected void grdExpenses_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        string ExpID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ExpID"].ToString();

        TextBox txtExpID = (TextBox)e.Item.FindControl("txtExpID");
        TextBox txtCategory = (TextBox)e.Item.FindControl("txtCategory");
        TextBox txtDescription = (TextBox)e.Item.FindControl("txtDescription");
        TextBox txtAmount = (TextBox)e.Item.FindControl("txtAmount");

        string strUpdate = "UPDATE expenses SET Category='" + txtCategory.Text + "',Description='" + txtDescription.Text + "',Amount='" + txtAmount.Text + "' WHERE ExpID='" + ExpID + "' ";
        MySqlCommand cmd = new MySqlCommand(strUpdate, conn);

        conn.Open();
        try
        {
            cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
    }
    protected void grdExpenses_ItemCommand(object sender, GridCommandEventArgs e)
    {


    }
    protected void grdExpenses_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        string ExpID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ExpID"].ToString();

        string strUpdate = "DELETE FROM expenses WHERE ExpID='" + ExpID + "' ";
        MySqlCommand cmd = new MySqlCommand(strUpdate, conn);
        conn.Open();
        try
        {
            cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
    }
}